package cn.clwxy.gdz.service;

import java.util.Date;

import cn.clwxy.gdz.bo.NewOrderByStudent;
import cn.clwxy.gdz.dao.OrderDao;
import cn.clwxy.gdz.dao.OrderDaoImpl;
import cn.clwxy.gdz.dao.OrderSubjectDao;
import cn.clwxy.gdz.dao.OrderSubjectDaoImpl;
import cn.clwxy.gdz.dao.StudentSubjectDao;
import cn.clwxy.gdz.dao.StudentSubjectDaoImpl;
import cn.clwxy.gdz.entity.Order;
import cn.clwxy.gdz.entity.OrderSubjectrelative;
import cn.clwxy.gdz.entity.StudentSubjectRelative;
import cn.clwxy.gdz.util.BaseDao;

public class CreatNewOrderByStudentServiceImpl extends BaseDao implements
		CreateNewOrderByStudentService {

	@Override
	public Integer createNewOrderByStudent(NewOrderByStudent newOrderByStudent) {

		try {
//			// 1�οͣ��½�һ��ѧ���û�
//			String sql = "INSERT INTO student(studentName,studentPhone,studentGradeId)value(?,?,?)";
//			int i = super.update(sql, newOrderByStudent.getUserName(),
//					newOrderByStudent.getUserPhone(),
//					newOrderByStudent.getGradeId());
//			System.out.println("����ѧ��������" + i);
//			// ��ѧ���Ŀ��ϵ������û�
//			// 2ͨ��ѧ��ĵ绰������ѧ���id
//			LoginService loginService = new LoginServiceImpl();
//			System.out.println(newOrderByStudent.getUserPhone());
//			Integer studentId = loginService
//					.getStudentIdByPhone(newOrderByStudent.getUserPhone());
//			System.out.println("�ҵ�����ѧ��" + studentId);
//			StudentSubjectRelative studentSubjectRelative = new StudentSubjectRelative();
//			StudentSubjectDao studentSubjectDao = new StudentSubjectDaoImpl();
//			System.out.println("����ѧ���Ŀ��ϵ��");
//			for (Integer subjectId : newOrderByStudent.getSubjectIds()) {
//				studentSubjectRelative.setStudentId(studentId);
//				studentSubjectRelative.setSubjectId(subjectId);
//				int i2 = studentSubjectDao
//						.addStudentSubjectRelative(studentSubjectRelative);
//				System.out.println(i2);
//			}
			// 3����������
			Order order = new Order();
			order.setIsPay(0);
			order.setIsPushToIndex(0);
			Date orderBeginTime = new Date();
			order.setOrderBeginTime(orderBeginTime);
			order.setStudentId(newOrderByStudent.getStudentId());
			order.setTeacherId(newOrderByStudent.getTeacherId());
			OrderDao orderDao = new OrderDaoImpl();
			Integer orderId = orderDao.addOrder(order);
			System.out.println(orderId);
			// �е�С����
			// 4�������������Ŀ�Ĺ�ϵ
			OrderSubjectrelative orderSubjectrelative = new OrderSubjectrelative();
			orderSubjectrelative.setOrderId(orderId);
			OrderSubjectDao orderSubjectDao = new OrderSubjectDaoImpl();
//			System.out.println("���붩����Ŀ��ϵ");
			for (Integer subjectId : newOrderByStudent.getSubjectIds()) {
				orderSubjectrelative.setSubjectId(subjectId);
				int i4 = orderSubjectDao.addOrderSubject(orderSubjectrelative);
				System.out.println(i4);
			}
			return orderId;
		} catch (Exception e) {
			return 0;
		}
	}
}
